package mvc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ActionMap;

import mvc.action.Action;
import mvc.action.LoginAction;
import mvc.action.ZhuceAction;
import mvc.form.ActionForm;
import mvc.form.LoginForm;
import mvc.util.FullBean;

/**
 * Servlet implementation class ActionServlet
 */
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("sign"+request.getParameter("sign"));
		String sign =request.getParameter("sign");
		ActionForm form = (ActionForm)FullBean.full( request);
		System.out.println(form.toString());
		Action action = null;
		/*if(sign.equals("mvc.form.Zhuce")){
			action=new ZhuceAction();
		}else if(sign.equals("mvc.form.LoginForm")){
			action=new LoginAction();
		}*/
		String actionname = ActionMapping.getMap().get(sign);
		Class clazz;
		try {
			clazz = Class.forName(actionname);
			action=(Action) clazz.newInstance();
		} catch ( Exception e) {
			e.printStackTrace();
		}
		String message=action.excute(form);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
